package com.example.demo.xml;

import com.example.demo.Constants;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class HelloFilter extends HttpFilter {

    private String message;

    @Override
    public void init() {
        System.out.printf(Constants.WEB_COMPONENT_IS_INIT, "Filter");
        message = String.format(Constants.HELLO_TEMPLATE, "Filter");
    }

    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
        res.setContentType("text/html");

        // Hello
        PrintWriter out = res.getWriter();
        out.println("<html><body>");
        out.println("<h1>" + message + "</h1>");
        out.println("</body></html>");
    }
}
